package ly.gupao.edu.thread.ExecuteByOrder;

import java.util.concurrent.Semaphore;

/**
 * @author: liyang
 * @date：2019/3/11 0011
 * describe：
 * ReentrantLock、Condition、CountDownLatch、Semaphore
 */
public class ConcurrentSemaphore {

    private static Semaphore s1 = new Semaphore(1);
    private static Semaphore s2 = new Semaphore(1);
    private static Semaphore s3 = new Semaphore(1);

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            while (true) {
                try {
                    s1.acquire();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("A");
                s2.release();
            }
        });
        Thread t2 = new Thread(() -> {
            while (true) {
                try {
                    s2.acquire();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("B");
                s3.release();
            }
        });
        Thread t3 = new Thread(() -> {
            while (true) {
                try {
                    s3.acquire();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("C");
                s1.release();
            }
        });
        s2.acquire();
        s3.acquire();
        t2.start();
        t3.start();
        t1.start();
    }

}
